package com.xjt.sentinel.controller.degradle;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author: xu
 * @Description: TODO
 * @Date: 2021/11/14 17:29
 */
@RestController
public class degradleController
{
	@SentinelResource(value = "testDemotion",blockHandler = "testDemotionBlock")
	@GetMapping("/testDemotion/{id}")
	public String testDemotion(@PathVariable("id") int id)
	{
		int i = 10/id;
		return "这是测试服务降级得测试接口";
	}

	@SentinelResource(value = "testDemotionRt",blockHandler = "testDemotionBlock")
	@GetMapping("/testDemotionRt/{id}")
	public String testDemotionRt(@PathVariable("id") int id) throws InterruptedException
	{
//		测试慢调用，降级处理
		Thread.sleep(10000);
		return "这是测试服务降级得测试接口";
	}

	public String testDemotionBlock(int id,BlockException e)
	{
		e.printStackTrace();
		return "当前接口已进行降级";
	}

	public String testDegradle(int id,BlockException e)
	{
		e.printStackTrace();
		return "当前服务发生异常，已被熔断";
	}

}
